//
//  UpLoadImageConnectStarView.m
//  WULIHH
//
//  Created by tang on 16/4/26.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "UpLoadImageConnectStarView.h"

#import "iCarousel.h"
#import "StarIconView.h"

#import "Header.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

#import "WuLiRequestTypeStarAPI.h"



#import "SearchStarViewController.h"
@interface UpLoadImageConnectStarView ()<iCarouselDataSource, iCarouselDelegate>
{
    UIView *cycleview;
}



@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, assign) BOOL wrap;


@end


@implementation UpLoadImageConnectStarView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _wrap = YES;
        self.items = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}





- (void)reloadWithArray:(NSMutableArray *)array
{
    [self.items removeAllObjects];
    [self.items addObjectsFromArray:array];
    [_carousel reloadData];
}




- (void)initUI
{
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
    backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //	backgroundView.image = [UIImage imageNamed:@"background.png"];
    [self addSubview:backgroundView];
    
    //create carousel
    _carousel = [[iCarousel alloc] initWithFrame:self.bounds];
    _carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _carousel.type = iCarouselTypeLinear;
    _carousel.delegate = self;
    _carousel.dataSource = self;
    
    //add carousel to view
    [self addSubview:_carousel];
    
}





- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //    NSLog(@"%d", _items.count);
    return [_items count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    //    UILabel *label = nil;
    
    
    UIImageView *imageView = nil;
//    [cycleview removeFromSuperview];
    
    if (view == nil)
    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 0, 0, 40, 40)];
        //        [imageView setBackgroundColor:[UIColor redColor]];
        [imageView.layer setCornerRadius:20];
        [imageView.layer setMasksToBounds:YES];
        [imageView setOpaque:NO];
        //    [topImageView setImage:image];
        [view addSubview:imageView];
        
        view.contentMode = UIViewContentModeCenter;
    }
    
//            [nameLable setText:[[self.items objectAtIndex:index] valueForKey:@"name"]];
        [imageView setImageWithURL:[NSURL URLWithString:[[self.items objectAtIndex:index] valueForKey:@"iconUrl"]]];
    
    return view;
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}


- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return 70.0f;
}


- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return _wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}



- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSString *stringID = [NSString stringWithFormat:@"%@", [_items[index] valueForKey:@"id"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fabustar" object:stringID];
    [_carousel reloadData];
}



- (void)carouselDidScroll:(iCarousel *)carousel
{
    [_carousel reloadData];
}




- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel
{
//    NSLog(@"%@", self.items[carousel.currentItemIndex]);
//
    if (_items.count == 0)
    {
        
    }
    else
    {
        cycleview = [carousel currentItemView];
        [cycleview.layer setBorderWidth:2.0f];
        [cycleview.layer setCornerRadius:20.0];
        [cycleview.layer setBorderColor:[UIColor yellowColor].CGColor];
        
        
        NSString *stringID = [NSString stringWithFormat:@"%@", [_items[carousel.currentItemIndex] valueForKey:@"id"]];
        //    [_MUpLoadImageConnectStarViewDelegate selectStarIDWithConnect:stringID];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"fabustar" object:stringID];

    }
    
 
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
